package com.action;

import com.pojo.AreaPojo;
import com.service.IAreaService;
import com.service.impl.AreaService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/final")
public class FinalController {

    @Resource
    private IAreaService areaService;
    /**
     * @YIKOUJING
     * @author LXY
     * @Description 市
     * @createDate 2022/8/15
     * @createTime 21:31
     */
    @GetMapping("t/finalPage")
    public String tfinalPage(Model model) {
        List<AreaPojo> areaPojos = areaService.queryAllProvinces();
        model.addAttribute("provinceList", areaPojos);
        return "thymeleaf/finalPage";
    }

    @GetMapping("t/queryCityByParentCode")
    public String queryCityByParentCode(Model model, @RequestParam(value = "provinceCode", required = true) String provinceCode, @RequestParam(value = "selectCode", required = false) String selectCode) {
        List<AreaPojo> areaPojos = areaService.queryAreaByParentCode(provinceCode);
        model.addAttribute("cityList", areaPojos);
        model.addAttribute("selectCode", selectCode == null?"":selectCode);
        return "thymeleaf/city";
    }

    @GetMapping("t/queryAreaList")
    public String queryAreaList(Model model, @RequestParam(value = "code", required = true) String code) {
        List<AreaPojo> areaPojos = areaService.queryAreaByParentCode(code);
        model.addAttribute("areaList", areaPojos);
        return "thymeleaf/areaList";
    }

    @GetMapping("t/addAreaDialog")
    public String addAreaDialog(Model model) {
        return "jsp/areaAddDialog";
    }

    @GetMapping("t/saveArea")
    public String saveArea(Model model,  @RequestParam(value = "name") String name, @RequestParam(value = "parentCode") String parentCode) {
        areaService.saveArea(name, parentCode);
        List<AreaPojo> areaPojos = areaService.queryAreaByParentCode(parentCode);
        model.addAttribute("areaList", areaPojos);
        return "thymeleaf/areaList";
    }

    @GetMapping("f/queryAreaByParentCode")
    public String queryAreaByParentCode(Model model, @RequestParam(value = "cityCode", required = true) String cityCode, @RequestParam(value = "selectCode", required = false) String selectCode) {
        List<AreaPojo> areaPojos = areaService.queryAreaByParentCode(cityCode);
        model.addAttribute("areaList", areaPojos);
        model.addAttribute("selectCode", selectCode == null?"":selectCode);
        return "freemarker/area";
    }

    @GetMapping("f/queryInfoByCode")
    public String queryInfoByCode(Model model, @RequestParam(value = "code", required = true) String code) {
        AreaPojo areaPojo = areaService.queryAreaByCode(code);
        model.addAttribute("areaPojo", areaPojo);
        return "freemarker/areaInfo";
    }



    /**
     * @YIKOUJING
     * @author LXY
     * @Description 省份
     * @createDate 2022/8/15
     * @createTime 21:29
     */
    @GetMapping("j/finalPage")
    public String finalPage(Model model, @RequestParam(value = "provinceCode", required = false) String provinceCode) {


        return "jsp/finalPage";
    }

    @GetMapping("f/finalPage")
    public String ffinalPage(Model model, @RequestParam(value = "provinceCode", required = false) String provinceCode) {


        return "freemarker/finalPage";
    }

}
